home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / cfortune.zip / GNU2SRC.C < prev    next >
C/C++ Source or Header  |  1989-05-25  |  7KB  |  276 lines

  1. /*
  2.  * gnu2src -- convert GNU format "yow.lines" file to fortune(6) *.src format
  3.  *
  4.  * gnu2src converts Zippy the Pinhead sayings from GNU format (ASCII NULL
  5.  * between sayings, to fortune(6) *.src file format (single lines of %%
  6.  * between sayings.  Optionally it will append the author signature
  7.  * "-- Zippy the Pinhead" for incorporation into the fortunes.dat file.
  8.  *
  9.  * usage: gnu2src [-d] [-i] [-a] [in_file] [out_file]
  10.  *
  11.  * -d    issue debugging information
  12.  *
  13.  * -i    issue usage and copyright information
  14.  *
  15.  * -a    append "-- Zippy the Pinhead" after each saying
  16.  */
  17.  
  18. char *Copyright_Legend[] = {
  19.  " Written by Paul Pomes, University of Illinois, Computing Services Office",
  20.  " Copyright (C) 1987 by Paul Pomes and the University of Illinois Board",
  21.  " of Trustees",
  22.  " ",
  23.  " This program is distributed in the hope that it will be useful,",
  24.  " but without any warranty.  No author or distributor accepts",
  25.  " responsibility to anyone for the consequences of using it or for",
  26.  " whether it serves any particular purpose or works at all, unless",
  27.  " s/he says so in writing.",
  28.  " ",
  29.  " Everyone is granted permission to copy, modify and redistribute",
  30.  " this program under the following conditions:",
  31.  " ",
  32.  "    Permission is granted to anyone to make or distribute copies",
  33.  "    of program source code, either as received or modified, in any",
  34.  "    medium, provided that all copyright notices, permission and",
  35.  "    nonwarranty notices are preserved, and that the distributor",
  36.  "    grants the recipient permission for further redistribution as",
  37.  "    permitted by this document, and gives him and points out to",
  38.  "    him an exact copy of this document to inform him of his rights.",
  39.  " ",
  40.  "    Permission is granted to distribute this program in compiled",
  41.  "    or executable form under the same conditions applying for",
  42.  "    source code, provided that either",
  43.  "    A. it is accompanied by the corresponding machine-readable",
  44.  "       source code, or",
  45.  "    B. it is accompanied by a written offer, with no time limit,",
  46.  "       to give anyone a machine-readable copy of the corresponding",
  47.  "       source code in return for reimbursement of the cost of",
  48.  "       distribution.  This written offer must permit verbatim",
  49.  "       duplication by anyone.",
  50.  "    C. it is distributed by someone who received only the",
  51.  "       executable form, and is accompanied by a copy of the",
  52.  "       written offer of source code which he received along with it.",
  53.  " ",
  54.  " In other words, you are welcome to use, share and improve this",
  55.  " program.  You are forbidden to forbid anyone else to use, share",
  56.  " and improve what you give them.   Help stamp out software-hoarding!",
  57.  " ",
  58.  " UUCP:     {ihnp4,seismo,pur-ee,convex}!uiucdcs!uiucuxc!paul",
  59.  " Internet: paul@uxc.cso.uiuc.edu   BITNET: paul@uiucuxc",
  60.  " MILNET:   paul@uiucuxc.arpa       CSNET:  paul%uxc@uiuc.csnet",
  61.  " US Mail:  Univ of Illinois, CSO, 1304 W Springfield Ave, Urbana, IL  61801",
  62.  0
  63. };
  64.  
  65. /*
  66.  * $Log$
  67.  */
  68.  
  69. #ifndef lint
  70. static char    RcsId[] = "@(#)$Header$";
  71. #endif
  72.  
  73. #include    <stdio.h>
  74. #include    <strings.h>
  75.  
  76. #define        equal(s1, s2)    (strcmp (s1, s2) == 0)
  77. #define        skipline(f)    while (getc (f) != '\n')
  78.  
  79. #define        PROC                /* null; easy to find procs */
  80. #define        FALSE        0
  81. #define        TRUE        1
  82. #define        CHNULL        ('\0')
  83. #define        CPNULL        ((char *) NULL)
  84. #define        FILENULL    ((FILE *) NULL)
  85. #define        REG        register
  86. #define        BMASK        0377
  87.  
  88. #define        INTV        5
  89.  
  90. /*
  91.  * Miscellaneous global values:
  92.  */
  93.  
  94. char *usage[] = {
  95.     "usage: %s [-d] [-i] [-n interval] [in_file] [out_file]",
  96.     CPNULL,
  97. };
  98.  
  99. /* how program was invoked (argv[0]) for error messages */
  100. char        *myname;
  101.  
  102. /* debug messages printed if set (-d) */
  103. int        dflag = FALSE;
  104.  
  105. /* append author statement if set (-a) */
  106. int        aflag = FALSE;
  107.  
  108. PROC main (argc, argv)
  109. int    argc;
  110. char    **argv;
  111. {
  112.     extern int    optind;            /* from getopt () */
  113.     extern char    *optarg;        /* from getopt () */
  114.     int        option;            /* option "letter" */
  115.     int        i, j, k;        /* counters */
  116.     char        *ifname, *ofname;    /* name to use */
  117.  
  118.     /*
  119.      * BASENAME:  Full string, or past '/' if any:
  120.      */
  121.  
  122.     myname = ((myname = rindex (*argv, '/')) == CPNULL) ? *argv : (myname + 1);
  123.  
  124.     /*
  125.      * PARSE ARGUMENTS:
  126.      */
  127.  
  128.     while ((option = getopt (argc, argv, "dia")) != EOF) {
  129.         switch (option) {
  130.             case 'd':
  131.             dflag++;
  132.             fprintf (stderr, "%s: dflag = %d\n", myname, dflag);
  133.             break;
  134.  
  135.             case 'a':
  136.             aflag++;
  137.             break;
  138.  
  139.             case 'i':
  140.             Usage (1);
  141.             break;
  142.  
  143.             default:
  144.             Usage (0);
  145.         }
  146.     }
  147.     argc -= optind;            /* skip options */
  148.     argv += optind;
  149.  
  150.     /*
  151.      * READ FROM LIST OF FILES OR STDIN:
  152.      */
  153.  
  154.     if (argc >= 1) {        /* read from file, write to stdout */
  155.         ifname = *argv;
  156.         if (freopen (ifname, "r", stdin) == FILENULL)
  157.             Error ("can't open file \"%s\" to read it", ifname);
  158.         argc--; argv++;
  159.     }
  160.     if (argc >= 1) {        /* read from file, write to stdout */
  161.         ofname = *argv;
  162.         if (freopen (ofname, "w", stdout) == FILENULL)
  163.             Error ("can't open file \"%s\" to write it", ofname);
  164.         argc--; argv++;
  165.     }
  166.  
  167.     /*
  168.      * Read through the input file replacing each instance of \0 with
  169.      * \n, the signature string if aflag is set, and %%\n
  170.      */
  171.     
  172.     while ((i = getchar()) != EOF) {
  173.         if (i != '\0')
  174.             putchar(i);
  175.         else {
  176.             if ((i = getchar ()) != EOF && i != '\n')
  177.                 ungetc (i, stdin);
  178.             if (aflag)
  179.                 printf ("\n                -- Zippy the Pinhead\n%%%%\n");
  180.             else
  181.                 printf ("\n%%%%\n");
  182.         }
  183.     }
  184.  
  185.     /*
  186.      * FINISH UP:
  187.      */
  188.      exit (0);
  189. } /* main */
  190.  
  191. /*
  192.  * Usage -- print how to use message
  193.  *
  194.  * Print usage messages (char *usage[]) to stderr and exit nonzero.
  195.  * Each message is followed by a newline.
  196.  *
  197.  *    parameters:
  198.  *        full_text    (IN)    prints the copyright statement if set
  199.  *    returns:
  200.  *        none, exit (1)
  201.  *    side effects:
  202.  *        program terminates
  203.  *    deficiencies:
  204.  */
  205.  
  206. PROC Usage (full_text)
  207. int    full_text;
  208. {
  209.     REG int        which = 0;        /* current line */
  210.  
  211.     while (usage[which] != CPNULL) {
  212.         fprintf (stderr, usage[which++], myname);
  213.         putc ('\n', stderr);
  214.     }
  215.     fflush (stdout);
  216.     which = 0;
  217.     if (full_text) {
  218.         while (Copyright_Legend[which] != CPNULL)
  219.             printf ("%s\n", Copyright_Legend[which++]);
  220.     }
  221.     exit (1);
  222. } /* Usage */
  223.  
  224. /*
  225.  * Error -- print error message with program name
  226.  *
  227.  * Print an error message to stderr and exit nonzero.  Message is preceded
  228.  * by "<myname>: " using global char *myname, and followed by a newline.
  229.  *
  230.  *    parameters:
  231.  *        message (IN)    printf format string
  232.  *        arg1-4    (IN)    printf arguments
  233.  *    returns:
  234.  *        none, exit (1)
  235.  *    side effects:
  236.  *        program terminates
  237.  *    deficiencies:
  238.  */
  239.  
  240. /* VARARGS */
  241. PROC Error (message, arg1, arg2, arg3, arg4)
  242. char    *message;
  243. long    arg1, arg2, arg3, arg4;
  244. {
  245.     fprintf (stderr, "%s: ", myname);
  246.     fprintf (stderr, message, arg1, arg2, arg3, arg4);
  247.     putc ('\n', stderr);
  248.     exit (1);
  249. } /* Error */
  250.  
  251. /*
  252.  * Malloc -- a malloc with error checking
  253.  *
  254.  *    parameters:
  255.  *        size    (IN)    number of bytes to get
  256.  *    returns:
  257.  *        (char *) of first char of block, or
  258.  *        calls Error() upon error
  259.  *    side effects:
  260.  *        none
  261.  *    deficiencies:
  262.  */
  263.  
  264. char    *malloc ();
  265. char    *Malloc ();
  266.  
  267. PROC char * Malloc (size)
  268. unsigned    size;    /* bytes to get */
  269. {
  270.     char    *cp;    /* pointer to memory */
  271.  
  272.     if ((cp = malloc (size)) == CPNULL)
  273.         Error ("malloc %d bytes failed", size);
  274.     return (cp);
  275. } /* Malloc */
  276.